package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.*;
import com.lovo.invoicing.bean.vo.OutGoodsBean;
import com.lovo.invoicing.mapper.GoodsMapper;
import com.lovo.invoicing.mapper.UserMapper;
import com.lovo.invoicing.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsBean> implements IGoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    IBuyCartService buyCartService;
    @Autowired
    ISaleCartService saleCartService;
    @Autowired
    IBuyInfoService buyInfoService;
    @Autowired
    IBuyService buyService;
    @Autowired
    ISaleInfoService saleInfoService;
    @Autowired
    ISaleService saleService;
    @Autowired
    ILogService logService;

    @Autowired
    UserMapper userMapper;
    @Override
    public IPage<GoodsBean> queryGoodsByPage(PageBean<GoodsBean> pageBean) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        GoodsBean g = pageBean.getT();
        if (g!=null){
            if (g.getGoodsName()!=null && !"".equals(g.getGoodsName())){
                qw.like("goods_name",g.getGoodsName());
            }
            if (g.getGoodsType()!=0){
                qw.eq("goods_type",g.getGoodsType());
            }
        }

        if (pageBean.getStartNo()>0){
            qw.ge("inventory_no",pageBean.getStartNo());
        }
        if (pageBean.getEndNo()>0){
            qw.le("inventory_no",pageBean.getEndNo());
        }
        if (pageBean.getNoSalePrice()!=0){
            qw.eq("sale_price",0);
        }
        return baseMapper.selectPage(new Page<>(pageBean.getPageNo(), pageBean.getPageSize()),qw);
    }

    @Override
    public IPage<OutGoodsBean> queryOutGoodsByPage(Page page, String goodsName, int goodsType) {
        return goodsMapper.selectOutGoodsByPage(page,goodsName,goodsType);
    }

    @Override
    public void addGoods(GoodsBean goodsBean) {
        GoodsBean g =queryGoodsByGoodsNo(goodsBean.getGoodsNo());
        if(g==null){
            baseMapper.insert(goodsBean);
        }
    }

    @Override
    public GoodsBean queryGoodsById(int goodsId) {
        return baseMapper.selectById(goodsId);
    }

    //修改最新采购价
    @Override
    @Transactional
    public void updateBuyPrice(String goodsNo, double buyPrice) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        qw.eq("goods_no",goodsNo);
        GoodsBean g =  baseMapper.selectOne(qw);
        g.setBuyPrice(buyPrice);
        baseMapper.updateById(g);
    }

    @Override
    @Transactional
    public void updateSalePrice(int goodsId, int salePrice) {
        GoodsBean goodsBean = baseMapper.selectById(goodsId);
        goodsBean.setSalePrice(salePrice);
        baseMapper.updateById(goodsBean);
    }

    //修改库存数量
    @Override
    @Transactional
    public void updateInventoryNo(String goodsNo , int inventoryNo) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        qw.eq("goods_no",goodsNo);
        GoodsBean g =  baseMapper.selectOne(qw);
        g.setInventoryNo(g.getInventoryNo()+inventoryNo);
        baseMapper.updateById(g);
    }

    @Override
    @Transactional
    public void reduceInventoryNo(String goodsNo, int outNo) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        qw.eq("goods_no",goodsNo);
        GoodsBean g =  baseMapper.selectOne(qw);
        g.setInventoryNo(g.getInventoryNo()-outNo);
        baseMapper.updateById(g);
    }

    @Override
    public GoodsBean queryGoodsByGoodsNo(String goodsNo) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        if (goodsNo!=null && !"".equals(goodsNo)){
            qw.eq("goods_no",goodsNo);
             return baseMapper.selectOne(qw);
        }
        return null;
    }

    //出库
    @Override
    @Transactional
    public void outbound(SaleInfoBean saleInfoBean, int userId) {
        List<SaleCartBean> list = saleCartService.querySaleCarts(userId);
        saleInfoBean.setSaleDate(LocalDate.now());
       int saleInfoId = saleInfoService.addSaleInfo(saleInfoBean);
       List<SaleBean> sList = new ArrayList<>();
        for (SaleCartBean s : list) {
            reduceInventoryNo(s.getGoodsNo(),s.getSaleNo());
            SaleBean saleBean = new SaleBean();
            saleBean.setGoodsName(s.getGoodsName());
            saleBean.setGoodsBrand(s.getGoodsBrand());
            saleBean.setSalePrice(s.getSalePrice());
            saleBean.setSaleNo(s.getSaleNo());
            saleBean.setGoodsType(s.getGoodsType());
            sList.add(saleBean);
        }
        //批量插入到销售表
        saleService.addSaleBeans(sList,saleInfoId);
        saleCartService.deleteAllSaleCart(userId);
        //日志
        UserBean u =userMapper.selectById(userId);
        logService.addLog(u.getUserName(),"出库");
    }

    @Override
    public void storage( BuyInfoBean buyInfoBean, int userId) {
        List<BuyCartBean> list = buyCartService.queryBuyCarts(userId);
        buyInfoBean.setBuyDate(LocalDate.now());
        //插入采购信息
        int buyInfoId = buyInfoService.addBuyInfo(buyInfoBean);
        List<BuyBean> bList = new ArrayList<>();
        //修改库存数量和最新采购价
        for (BuyCartBean b : list) {
            updateBuyPrice(b.getGoodsNo(),b.getBuyPrice());
            updateInventoryNo(b.getGoodsNo(),b.getBuyNo());
            BuyBean buyBean =new BuyBean();
            buyBean.setGoodsName(b.getGoodsName());
            buyBean.setGoodsBrand(b.getGoodsBrand());
            buyBean.setBuyPrice(b.getBuyPrice());
            buyBean.setBuyNo(b.getBuyNo());
            bList.add(buyBean);
        }
        //批量插入到采购表
        buyService.addBuyBeans(bList,buyInfoId);
        //清空购物车
        buyCartService.deleteAllBuyCart(userId);
        //日志
        UserBean u =userMapper.selectById(userId);
        logService.addLog(u.getUserName(),"入库");
    }
}
